package metier.JDBC;
import java.sql.*;
import java.util.ArrayList;

import metier.*;

public class FormationJDBC extends Formation{


	/* Attributs prives */
	//Requete SQL permettant de mettre a jour la formation dans la base de donnees 
	private final static String updateQuery = "UPDATE Formation SET description_formation = ? where nom_formation = ?";
	//Requete SQL permettant de recuperer la liste de toutes les formation de la base de donnees
	private final static String selectQuery = "SELECT * FROM Formation";
	//Requete SQL permettant de recuperer les informations de la formation dans la base de donnees
	private final static String loadQuery = "SELECT * FROM formation WHERE nom_formation = ?";
	//Requete SQL permettant de supprimer les informations de la formation dans la base de donnees
	private final static String deleteQuery="DELETE FROM formation WHERE nom_formation= ?";
	//Requete SQL permettant d'inserer les informations de la formation dans la base de donnees
	private final static String insertQuery="INSERT INTO formation VALUES (?,?)";
	
	public FormationJDBC(String nom,String description) {
		super(nom,description);
	}

        public FormationJDBC(){
            super();
        }
	

	
	public void delete() throws ExceptionMetier {
		try {
			Connection con = ConnectionJDBC.getConnection();
			PreparedStatement sta = con.prepareStatement(deleteQuery);
			//R�cup�re toutes les seances existantes dans la base de donn�es
	        sta.setString(1,this.getNom());
	        sta.executeUpdate();
		}
		catch (ClassNotFoundException ex) {throw new ExceptionMetier("Delete failed", ex);} 
	    catch (SQLException ex) {throw new ExceptionMetier("Delete failed", ex);}
	}

	/* (non-Javadoc)
	 * @see metier.Seance#insert()
	 */
	@Override
	public void insert() throws ExceptionMetier {
		try {
			Connection con = ConnectionJDBC.getConnection();
            PreparedStatement sta = con.prepareStatement(insertQuery);
                       
            sta.setString(1, getNom());
            sta.setString(2, getDescription());
            sta.executeUpdate();
        } 
		catch (ClassNotFoundException ex) {throw new ExceptionMetier("Insert failed", ex);} 
        catch (SQLException ex) {throw new ExceptionMetier("Insert failed", ex);}
	}

	/* (non-Javadoc)
	 * @see metier.Seance#update()
	 */
	@Override
	public void update() throws ExceptionMetier {
		try {
            Connection con = ConnectionJDBC.getConnection();
            PreparedStatement update = con.prepareStatement(updateQuery);
            update.setString(1, getDescription());
            update.setString(2, getNom());
            update.executeUpdate();
          
        } 
		catch (ClassNotFoundException ex) {throw new ExceptionMetier("Update failed", ex);} 
		catch (SQLException ex) {throw new ExceptionMetier("Update failed", ex);}
	}
	

    @Override
    public void load(String nom) throws ExceptionMetier {
        try {
            Connection con = ConnectionJDBC.getConnection();
            setNom(nom);
            PreparedStatement psta = con.prepareStatement(loadQuery);
            psta.setString(1, getNom());
            ResultSet res = psta.executeQuery();
            if (!res.next()) {
                throw new ExceptionMetier("Load failed due to non existing Object");
            }
            if (!res.getString("nom_formation").equals(getNom())) {
                throw new ExceptionMetier("Load failed due to non existing Object");
            }
            setNom(res.getString("nom_formation"));
            setDescription(res.getString("description_formation"));
            res.close();
            psta.close();

        }
		catch (ClassNotFoundException ex) {throw new ExceptionMetier("Load failed", ex);}
        catch (SQLException ex) {throw new ExceptionMetier("Load failed BD", ex);}
    }


    
    public static ArrayList <ElementListe> select() throws ExceptionMetier {
		ArrayList <ElementListe> listeSalle = new ArrayList<ElementListe>();
		
		try {
		    ResultSet res = null;
		    Connection con = ConnectionJDBC.getConnection();
		    Statement s = con.createStatement();
		    res = s.executeQuery(selectQuery);System.out.println("bebertt");
		    
			// Ajoute les salles une par une a la liste
			while (res.next())
			{		
				
				listeSalle.add(new ElementListe(0,res.getString("nom_formation")));
			}
		    return listeSalle;
		} 
		catch (ClassNotFoundException ex) {throw new ExceptionMetier("Select failed", ex);} 
		catch (SQLException ex) {throw new ExceptionMetier("Select failed", ex);}
	}
	
}
